LootMe = {}

local version = "0.1.2";

local onLootWindowShown
local isLootMeRunning

function LootMe.Initialize()
  LootMe.Disable()

  onLootWindowShown = EA_Window_Loot.OnShown
  EA_Window_Loot.OnShown = LootMe.OnLootWindowShown

  LibSlash.RegisterSlashCmd("lootme", LootMe.HandleSlashCommand)	
end

function LootMe.OnLootWindowShown(...)
  if not isLootMeRunning then
    return onLootWindowShown(...)
  end

  -- do stuff here
  EA_Window_Loot.OnLootAll()
  return onLootWindowShown(...)
end

function LootMe.parseArguments(delimiter,str)
  if (delimiter=='') then 
    return false 
  end
  
  local pos = 0
  local arr = {}
  for st,sp in function() return string.find(str,delimiter,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) 
    pos = sp + 1
  end
  table.insert(arr,string.sub(str,pos))
  return arr
end

function LootMe.HandleSlashCommand(input)
  local args = LootMe.parseArguments(" ",input)
  if args[1] == "help" then
    LootMe.DisplayHelp();
  elseif args[1] == "on" then
    LootMe.Enable()
  elseif args[1] == "off" then
    LootMe.Disable()
  elseif args[1] == "status" then
    LootMe.DisplayStatus()
  else
    LootMe.DisplayHelp();
  end  
end

function LootMe.DisplayHelp()
  LootMe.WriteToChat("LootMe addon Help")
  LootMe.WriteToChat("Version: "..version)
  LootMe.DisplayStatus()
  LootMe.WriteToChat("Usage: /lootme [arguments]")
  LootMe.WriteToChat("/lootme help - display this help message")
  LootMe.WriteToChat("/lootme [on/off] - turn the LootMe addon on/off")
  LootMe.WriteToChat("/lootme status - display the current status for LootMe")
end

function LootMe.Enable()
  isLootMeRunning = true
  LootMe.WriteToChat("LootMe addon is enabled")
end

function LootMe.Disable()
  isLootMeRunning = false
  LootMe.WriteToChat("LootMe addon is disabled")
end

function LootMe.DisplayStatus()
  if isLootMeRunning then
    LootMe.WriteToChat("Status: LootMe enabled")
  else 
    LootMe.WriteToChat("Status: LootMe disabled")
  end
end

function LootMe.WriteToChat(text)
  EA_ChatWindow.Print(towstring(text))
end
